﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Common; 

public partial class website_admin_knowlegebase_kbcategorymagviewer : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin("system configuration");

        switch (RequestBase.GetFormString("otype"))
        {
            case "loadingtreedata":
                LoadingTreeData();
                break;
            case "delete":
                Delete();
                break;
        }
    }

    protected string CategoryTree = "";
    /// <summary>
    /// 异步加载目录树
    /// </summary>
    void LoadingTreeData()
    {
        DataTable datCategoryTable = BLL.CommonBLL.Select("KbCategoryBase", "*", " AND DeletionStateCode = 0 ORDER BY LineNum ASC");

        try
        {
            /// 加载WBS树
            InitTree(datCategoryTable, "00000000-0000-0000-0000-000000000000");
            BLL.AjaxHelper.ResponseWrite(CategoryTree);
        }
        catch (Exception err) { }
    }
    void InitTree(DataTable dat, string parentId)
    {
        /// 父节点
        string htmlItem1 = "<tr class=\"trTreeBoxHeader\" onmouseover=\"this.style.borderColor='#C0C0C0';this.style.color='#000000';this.style.backgroundColor ='#C3DDFF'\" onmouseout=\"this.style.borderColor='';this.style.color='';this.style.backgroundColor =''\">";
        htmlItem1 += "<td><div style=\"text-align: left; \"><img src=\"../_imgs/admin/minus.gif\" vtype=\"minus\" id=\"img_{1}\" onclick=\"TreeNoteShow(this);\" notetype=\"{0}\" categoryid=\"{1}\" oname=\"imgTreeNoteIco\" />";
        htmlItem1 += "{2}</div><!--名称--></td>";
        htmlItem1 += "<td><div style=\"text-align: center; \">{3}</div><!--更新时间--></td>";
        htmlItem1 += "<td><div style=\"text-align: left; \">{4}</div><!--操作--></td>";
        htmlItem1 += "</tr>";

        /// 子节点
        string htmlItem2 = "<tr class=\"trTreeBoxHeader\" onmouseover=\"this.style.borderColor='#C0C0C0';this.style.color='#000000';this.style.backgroundColor ='#C3DDFF'\" onmouseout=\"this.style.borderColor='';this.style.color='';this.style.backgroundColor ='#ffffff'\" style=\"background: #ffffff;\">";
        htmlItem2 += "<td><img src=\"../_imgs/admin/defaultnode.gif\" />";
        htmlItem2 += "<div>{0}</div><!--名称--></td>";
        htmlItem2 += "<td><div style=\"text-align: center; \">{1}</div><!--类型--></td>";
        htmlItem2 += "<td><div style=\"text-align: left; \">{2}</div><!--操作--></td></tr>";

        if (dat == null)
            return;

        DataRow[] dr = dat.Select("ParentId = '" + parentId + "'");
        if (dr.Length > 0)
        {
            int layer = int.Parse(dat.Select("ParentId = '" + parentId + "'")[0]["Layer"].ToString());

            int tbWidth = 782 - (layer * 20 + 1 + layer);

            string bgcolor = "#ffffff";
            string fontcolor = "#333333";

            /// 如果是根节点
            if (parentId == "{00000000-0000-0000-0000-000000000000}")
                CategoryTree += "<tr class=\"trTreeAreaItems\" id=\"tr_" + parentId + "\"><td colspan=\"3\" class=\"body\" style=\"border-bottom: 0px; height: 100%;\">";
            else
                CategoryTree += "<tr class=\"trTreeBoxItems\" id=\"tr_" + parentId + "\"><td colspan=\"3\" class=\"body\">";

            CategoryTree += "<table class=\"tbTreeBox\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\" style=\"width: " + tbWidth + "px; background:" + bgcolor + "; color: " + fontcolor + ";\"><!--顶级970px,按层级递减1px-->";
            CategoryTree += "<colgroup>";
            CategoryTree += "<col />";
            CategoryTree += "<col style=\"width: 120px; text-align: center;\" />";
            CategoryTree += "<col style=\"width: 220px; text-align: center; \" />";
            CategoryTree += "</colgroup>";

            foreach (DataRow d in dr)
            {
                if (d["NodeType"].ToString().ToLower().Trim() == "childnode" && layer > 1)
                {
                    CategoryTree += string.Format(htmlItem2,
                        d["CategoryName"].ToString(),
                        d["CategoryType"].ToString().Replace("1", "列表").Replace("2", "内容"),
                        "<a href=\"javascript:void(0);\" onclick=\"OpenDia('addchildnode', '" + d["CategoryId"].ToString() + "');\" class=\"link_btn_add\">添加</a>|<a href=\"javascript:void(0);\" onclick=\"OpenDia('modify', '" + d["CategoryId"].ToString() + "');\" class=\"link_btn_edit\">修改</a>" + (d["IsProtected"].ToString().ToLower() == "true" ? "" : "|<a href=\"javascript:void(0);\" onclick=\"Delete('" + d["CategoryId"].ToString() + "');\" class=\"link_btn_delete\">删除</a>"));
                }
                else
                {
                    CategoryTree += string.Format(htmlItem1,
                        d["NodeType"].ToString(),
                        d["CategoryId"].ToString(),
                        d["CategoryName"].ToString(),
                        d["CategoryType"].ToString().Replace("1", "列表").Replace("2", "内容"),
                        "<a href=\"javascript:void(0);\" onclick=\"OpenDia('addchildnode', '" + d["CategoryId"].ToString() + "');\" class=\"link_btn_add\">添加</a>|<a href=\"javascript:void(0);\" onclick=\"OpenDia('modify', '" + d["CategoryId"].ToString() + "');\" class=\"link_btn_edit\">修改</a>" + (d["IsProtected"].ToString().ToLower() == "true" ? "" : "|<a href=\"javascript:void(0);\" onclick=\"Delete('" + d["CategoryId"].ToString() + "');\" class=\"link_btn_delete\">删除</a>"));
                }

                InitTree(dat, d["CategoryId"].ToString().Trim());
            }

            CategoryTree += "</tbody></table></td></tr>";
        }
    } 
    /// <summary>
    /// 删除目录
    /// </summary>
    void Delete()
    {
        string res = BLL.KbCategoryBase.Delete(
            RequestBase.GetFormString("objectId"),
            true
            );

        BLL.AjaxHelper.ResponseWrite(res);
    }
}
